package com.myStudent.test;

import com.myStudent.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManger {
    public static void main(String[] args) {

        // 创建容器
        ArrayList<Student> stuList = new ArrayList<>();
        Scanner sc = new Scanner(System.in);

        lo:
        do {
            //1.搭建主界面菜单
            System.out.println(" =================欢迎来到学生管理系统================= ");
            System.out.println("|                   1. 添加学生                      |");
            System.out.println("||                  2. 删除学生                     ||");
            System.out.println("||                  3. 修改学生                     ||");
            System.out.println("||                  4. 查询学生                     ||");
            System.out.println("||                  5. 打印全部信息                  ||");
            System.out.println("|                   6. 退  出                       |");
            System.out.println(" =================欢迎来到学生管理系统================= ");

            System.out.println("请输入选项进行操作······");

            int n = sc.nextInt();

            switch (n) {
                case 1: {
                    addStudent(stuList);
                    break;
                }
                case 2: {
                    removeStudent(stuList);
                    break;
                }
                case 3: {
                    updataStudent(stuList);
                    break;
                }
                case 4: {
                    selectStudent(stuList);
                    break;
                }
                case 5: {
                    Show(stuList);
                    break;
                }
                case 6: {
                    System.out.println("欢迎下次使用，再见！！！");
                    break lo;
                }
                default: {
                    System.out.println("没有该选项，请重新输入！");
                    break;
                }
            }
        } while (true);

    }

    // 打印容器中所有信息
    private static void Show(ArrayList<Student> stuList) {
        if(null == stuList || stuList.isEmpty()){
            System.out.println("无信息，请添加后重新查询！");
            return;
        }
        for (Student stu : stuList) {
            System.out.println(stu.getSid() + " " + stu.getName() + " " + stu.getAge() + " " + stu.getBirthday());
        }
    }

    // 返回学生索引，失败返回-1
    public static int getIndex(ArrayList<Student> stu, String sid){
        int index = -1;
        if(null != stu && !stu.isEmpty()){
            for(int i = 0; i < stu.size(); ++i){
                Student s = stu.get(i);
                if(sid.equals(s.getSid())) return i;
            }
        }
        return index;
    }

    // 修改学生
    private static void updataStudent(ArrayList<Student> stuList) {
        Scanner sc = new Scanner(System.in);
        int n = 0;
        // 1. 提示信息
        System.out.println("请输入需要修改的学生学号：");
        String sid = sc.next();
        // 2. 获取需要修改的学生对象
        int index = getIndex(stuList, sid);
        while (!sid.equals("0")) {
            if (index != -1) {
                System.out.println("当前学生信息为：");
                System.out.println(stuList.get(index).getSid() + " " + stuList.get(index).getName() + " "
                        + stuList.get(index).getAge() + " " + stuList.get(index).getBirthday());
                break;
            } else {
                System.out.println("没有该学生，请重新输入! 或 按 0 退出修改。");
                sid = sc.next();
                index = getIndex(stuList, sid);
            }
        }
        // 3. 修改
        if (sid.equals("0")) {
            System.out.println("修改失败！");
            return;
        }
        System.out.println("请输入更新的学生信息：");
        System.out.print("Sid：");
        stuList.get(index).setSid(sc.next());
        System.out.print("Name：");
        stuList.get(index).setName(sc.next());
        System.out.print("Age：");
        stuList.get(index).setAge(sc.nextInt());
        System.out.print("BirthDay：");
        stuList.get(index).setBirthday(sc.next());
        // 4. 打印修改后的学生信息
        // 5. 成功信息
        System.out.println("修改成功！");
        System.out.println(stuList.get(index).getSid() + " " + stuList.get(index).getName() + " "
                + stuList.get(index).getAge() + " " + stuList.get(index).getBirthday());
    }

    // 查询学生
    private static void selectStudent(ArrayList<Student> stuList) {
        Scanner sc = new Scanner(System.in);
        // 1.提示信息
        System.out.println("请输入学生学号进行查询：");
        String sid = sc.next();
        // 2.查询
        for (Student s : stuList) {
            if (s.getSid().equals(sid)) {
                System.out.println("查询成功！");
                System.out.println(s.getSid() + " " + s.getName() + " " + s.getAge() + " " + s.getBirthday());
                return;
            }
        }
        // 3.打印
        System.out.println("输入学号有误，查询失败！");
    }

    // 删除学生
    private static void removeStudent(ArrayList<Student> stuList) {
        Scanner sc = new Scanner(System.in);
        // 1. 给出录入提示信息
        // 2. 按条件删除
        System.out.println("请输入学号删除：");
        String sid = sc.next();
        for (int i = 0; i < stuList.size(); i++) {
            // 3. 删除容器中的对应信息
            if (stuList.get(i).getSid().equals(sid)) {
                System.out.println("删除成功！");
                System.out.println(stuList.get(i).getSid() + "  " + stuList.get(i).getName() + "  "
                        + stuList.get(i).getAge() + "  " + stuList.get(i).getBirthday());
                stuList.remove(i);
                return;
            }
        }
        // 4. 打印删除成功信息
        System.out.println("未找到您输入的学号，删除失败！");
    }

    // 添加学生
    public static void addStudent(ArrayList<Student> stuList) {
        Scanner sc = new Scanner(System.in);
        // 1.给出录入的提示信息
        int index = -1;
        String sid = null;
        do{
            if(index != -1){
                System.out.println("您输入的学号已存在，请勿重复添加！");
                System.out.println("请重新输入学号进行添加，按任意数继续添加，按 0 退出添加！");
                if(sc.nextInt() == 0) {
                    System.out.println("添加失败！");
                    return;
                }
            }
            System.out.println("请输入学号：");
            sid = sc.next();
            index = getIndex(stuList, sid);
        }while (index != -1);
        System.out.println("请输入姓名：");
        String name = sc.next();
        System.out.println("请输入年龄：");
        int age = sc.nextInt();
        System.out.println("请输入生日：");
        String brithday = sc.next();
        // 2.将键盘录入的信息封装为学生对象
        Student stu = new Student(sid, name, age, brithday);
        // 3.将封装好的学生对象，添加到集合容器当中
        stuList.add(stu);
        // 4.给出添加成功的提示信息
        stu = stuList.get(stuList.size() - 1);
        System.out.println(stu.getSid() + " " + stu.getName() + " " + stu.getAge() + " " + stu.getBirthday());
        System.out.println("添加成功！");
    }
}

